package com.huawei.esdk.tp.business.common.sitectrl;

import java.util.List;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.base.BaseService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInfoCommService;
import com.huawei.esdk.tp.domain.model.bean.CameraControl;

public class SiteCtrlService extends BaseService
{
    private static final Logger LOGGER = Logger.getLogger(SiteCtrlService.class);
    
    private SiteInfoCommService siteInfoCommService;

    public SDKErrorCode ctrlAuxStream(String uri, Integer controlCode)
            throws SDKException
    {
        LOGGER.debug("uri = " + uri);
        SDKErrorCode result = null;

        // 根据用户输入的会场URI，判断会场是否存在，不存在则返回
        if (null == uri || uri.isEmpty())
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (null == controlCode)
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return result;
        }
        boolean exist = siteInfoCommService.containSite(uri);
        if (!exist)
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SITE_NOT_EXIST_ERRORCODE);
            return result;
        }
        // 判断在SDK设备配置列表中是否存在该会场
        exist = siteInfoCommService.containSiteDevice(uri);
        if (!exist)
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SITE_NOT_EXIST_IN_DEVICEMAP_ERRORCODE);
            return result;
        }

        // 准备Model
        Site site = new Site();
        site.setUri(uri);
        
        // 调用Model

        if (0 == controlCode)
        {
            result = site.getStreamOperator().startSendAuxStream();
        }
        else if (1 == controlCode)
        {
            result = site.getStreamOperator().stopSendAuxStream();
        }
        else
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.PARAM_OUTOFRANGE_ERRORCODE);
        }
        return result;
    }

    public SDKErrorCode ctrlCamera(String uri, CameraControl parameters)
            throws SDKException
    {
        SDKErrorCode result = null;

        // 根据用户输入的会场URI，判断会场是否存在，不存在则返回
        if (null == uri || uri.isEmpty())
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        boolean exist = siteInfoCommService.containSite(uri);
        if (!exist)
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SITE_NOT_EXIST_ERRORCODE);
            return result;
        }
        // 判断在SDK设备配置列表中是否存在该会场
        exist = siteInfoCommService.containSiteDevice(uri);
        if (!exist)
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SITE_NOT_EXIST_IN_DEVICEMAP_ERRORCODE);
            return result;
        }
        //必填参数CameraControl校验
        if(null == parameters)
        {
            result = new SDKErrorCode();
            result.setErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return result;
        }

        // 准备Model
        Site site = new Site();
        site.setUri(uri);
        // 调用Model
        result = site.getCamera().ctrlCameraOpeate(parameters);

        return result;
    }

    public SDKErrorCode connectSites(String confID, List<String> sitesUri)
            throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        // 校验必填参数
        if (StringUtils.isEmpty(confID))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (null == sitesUri || sitesUri.isEmpty())
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confID))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }

        // 准备Model
        ScheduleConference scheduleConf = new ScheduleConference(confID);
        // 调用Model
        result = scheduleConf.connectSites(sitesUri);

        return result;
    }

    public SDKErrorCode disconnectSites(String confID, List<String> sitesUri)
            throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        // 校验必填参数
        if (StringUtils.isEmpty(confID))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (null == sitesUri || sitesUri.isEmpty())
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confID))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }

        // 准备Model
        ScheduleConference scheduleConf = new ScheduleConference(confID);
        // 调用Model
        result = scheduleConf.disconnectSites(sitesUri);

        return result;
    }

    public SDKErrorCode quietSites(String confID, List<String> sitesUri,
            Boolean actionCode) throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        // 校验必填参数
        int rtn = paramValide(confID, sitesUri, actionCode);
        if (0 != rtn)
        {
            result.setErrCode(rtn);
            return result;
        }
        // 准备Model
        ScheduleConference scheduleConf = new ScheduleConference(confID);
        // 调用Model
        result = scheduleConf.quietSites(sitesUri, actionCode);

        return result;
    }

    public SDKErrorCode muteSites(String confID, List<String> sitesUri,
            Boolean actionCode) throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        // 校验必填参数
        int rtn = paramValide(confID, sitesUri, actionCode);
        if (0 != rtn)
        {
            result.setErrCode(rtn);
            return result;
        }

        // 准备Model
        ScheduleConference scheduleConf = new ScheduleConference(confID);
        // 调用Model
        result = scheduleConf.muteSites(sitesUri, actionCode);
        return result;
    }

    private int paramValide(String confID, List<String> sitesUri,
            Boolean actionCode)
    {
        // 校验必填参数
        if (StringUtils.isEmpty(confID))
        {
            return ErrInfo.CONF_ID_ISNULL_ERRORCODE;
        }
        if (null == sitesUri || sitesUri.isEmpty())
        {
            return ErrInfo.SITE_URI_ISNULL_ERRORCODE;
        }
        if (!StringUtils.isUintNumber(confID))
        {
            return ErrInfo.CONF_ID_IS_INVALID_ERRORCODE;
        }
        if (null == actionCode)
        {
            return ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE;
        }
        return 0;
    }

    public SiteInfoCommService getSiteInfoCommService()
    {
        return siteInfoCommService;
    }

    public void setSiteInfoCommService(SiteInfoCommService siteInfoCommService)
    {
        this.siteInfoCommService = siteInfoCommService;
    }
}
